package com.zzz.robot201.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.zzz.robot201.R;
import com.zzz.robot201.app.base.BaseViewActivity;
import com.zzz.robot201.app.config.AppContent;
import com.zzz.robot201.di.component.DaggerRegisterComponent;
import com.zzz.robot201.mvp.contract.RegisterContract;
import com.zzz.robot201.mvp.model.api.bean.PhoneCode;
import com.zzz.robot201.mvp.model.api.bean.Register;
import com.zzz.robot201.mvp.presenter.RegisterPresenter;
import com.zzz.robot201.utils.CountDownTimerUtils;
import com.zzz.robot201.utils.PhoneUtil;
import com.zzz.robot201.utils.SpUtils;
import com.zzz.robot201.utils.ToastUtil;
import com.zzz.robot201.utils.UIUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class RegisterActivity extends BaseViewActivity<RegisterPresenter> implements RegisterContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_back)
    Button btnBack;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.cbox_agree)
    CheckBox cboxAgree;
    @BindView(R.id.tv_gologin)
    TextView tvGologin;
    private boolean isCheck = false;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerRegisterComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_register; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setTitle(tvTitle, UIUtils.getString(R.string.title_register));
        cboxAgree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (cboxAgree.isChecked()) {
                    isCheck = true;
                } else {
                    isCheck = false;
                }
            }
        });
    }

    @Override
    public void showLoading() {
        showBaseLoading();
    }

    @Override
    public void hideLoading() {
        hideBaseLoading();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ToastUtil.showToast(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick({R.id.btn_back, R.id.tv_code, R.id.btn_register, R.id.cbox_agree, R.id.tv_gologin})
    public void onViewClicked(View view) {
        if (UIUtils.isFastClick()) return;
        switch (view.getId()) {
            case R.id.btn_back:
                killMyself();
                break;
            case R.id.tv_code:
                String phone = etPhone.getText().toString();
                if (PhoneUtil.isMobileNO(phone)) {
                    if (mPresenter != null) mPresenter.getPhoneCode(phone, "regCode");
                } else {
                    ToastUtil.showToast(UIUtils.getString(R.string.phone_number));
                }
                break;
            case R.id.btn_register:
                String phoneNumber = etPhone.getText().toString();
                String code = etCode.getText().toString();
                String passWord = etPassword.getText().toString();
                if (!TextUtils.isEmpty(passWord)) {
                    if (isCheck == false) {
                        ToastUtil.showToast(UIUtils.getString(R.string.isCheck));
                    } else {
                        if (mPresenter != null) mPresenter.Register(phoneNumber, code, passWord);
                    }
                } else {
                    ToastUtil.showToast(UIUtils.getString(R.string.password));
                }

                break;
            case R.id.cbox_agree:

                break;
            case R.id.tv_gologin:
                launchActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void phoneCode(PhoneCode code) {
        if (code.isSuccess()){
            // 倒计时
            CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tvCode, 60000, 1000);
            mCountDownTimerUtils.start();
            ToastUtil.showToast(code.getMsg());
        }

    }

    @Override
    public void register(Register code) {
        SpUtils.put(this,AppContent.SaveInfoKey.TOKEN,code.getData().getToken());
        SpUtils.put(this, AppContent.User.INFO, code.getData());
        finish();
    }
}
